import { StyleSheet, Text, View } from 'react-native'
import React, { useEffect, useState } from 'react'
import Socket from './components/Socket'
import { useSearchParams } from 'expo-router/build/hooks'
import AsyncStorage from '@react-native-async-storage/async-storage';


const chat = () => {
    const [_id, mingname] = useSearchParams()
    console.log(mingname[1], _id[1])

    interface User1 {
        users: string,
        names: string,
        password: string,
        phone: string,
        focusOn: Array<string>,
    }
    const [userOne, setUserOne] = useState<User1>()

    useEffect(() => {
        getUser()
    }, [])
    const getUser = async () => {
        getData('user')
    }

    const getData = async (key: string) => {
        try {
            const value: any = await AsyncStorage.getItem(key);
            if (value !== null) {
                setUserOne(JSON.parse(value))
                console.log(value?.names)
            } else {
                console.log('No data found for the key')
            }
            return value ? JSON.parse(value) : null;
        } catch (error) {
            console.error('Failed to fetch data', error);
        }
    };

    return (
        <View>
            <Socket username={mingname[1] == userOne?.names ? userOne?.names : mingname[1]} _id={_id[1]} />
        </View>
    )
}

export default chat

const styles = StyleSheet.create({})